package com.example.demo.util;


import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import com.example.demo.entity.User;

/**
 * 密码工具类
 * @author Henry.Y
 * 2019年5月23日
 */
public class PasswordUtil {

	private static final String algorithmName= "md5";
	
	private static final int hashIterations= 2;
	
	public static void encryptPassword(User user){
		RandomNumberGenerator randomNumberGenerator= new SecureRandomNumberGenerator();
		user.setSalt(randomNumberGenerator.nextBytes().toHex());
		String newPassword= new SimpleHash(algorithmName,user.getPassword(),ByteSource.Util.bytes(user.getCredentialSalt()),hashIterations).toHex();
		user.setPassword(newPassword);
	}	
}
